﻿using System;

namespace TopHero
{
    public class MonsterBattleState : Singleton<MonsterBattleState>, State<Monster>
    {
        public void Enter(Monster t)
        {
            t.SetPropertyValue(PropertyId.SPEED, 0);
        }

        public void Execute(Monster t)
        {
            if (t.GetPropertyValue(PropertyId.SPEED) >= t.GetPropertyValue(PropertyId.MAX_SPEED))
            {
                t.SetPropertyValue(PropertyId.SPEED, 0);
                Hero hero = t.world.hero;
                MessageDispatcher.Instance.DispatchMsg(0, t.ID(), hero.ID(), MessageType.Attack,
                    t.GetPropertyValue(PropertyId.ATK));
            }
        }

        public void Exit(Monster t)
        {
            t.SetPropertyValue(PropertyId.SPEED, 0);
        }

        public bool OnMessage(Monster t, Telegram msg)
        {
            if (msg.msg == MessageType.Attack)
            {
                float damage = (float) msg.extraInfo;
                float hp = t.GetPropertyValue(PropertyId.HP);
                hp = Math.Max(0, hp - damage);
                t.SetPropertyValue(PropertyId.HP, hp);
                return true;
            }

            return false;
        }
    }
}